


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>构建镜像</title>
    <meta name="renderer" content="webkit">
    <!--<meta name="referrer" content="never">-->
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../../layuiadmin/layui/css/layui.css" media="all">
</head>
<body>

<div class="layui-form" lay-filter="layuiadmin-form-order" id="layuiadmin-form-order" style="padding: 20px 30px 0 0;">
    <div class="layui-form-item">
        <label class="layui-form-label">应用名称</label>
        <div class="layui-input-inline">
            <input type="text" id="jobName" name="jobName" lay-verify="required" value="erp-beta.zfzj.api" disabled="disabled" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">代码分支</label>
        <div class="layui-input-inline">
            <input type="text" id="branchName" name="branchName" lay-verify="required" value="Beta" disabled="disabled" autocomplete="off" class="layui-input">
        </div>
    </div>
    <!--<div class="layui-form-item">-->
        <!--<label class="layui-form-label">工单进度</label>-->
        <!--<div class="layui-input-inline">-->
            <!--<input type="text" name="progress" lay-verify="required" placeholder="工单进度..." autocomplete="off" class="layui-input">-->
        <!--</div>-->
    <!--</div>-->
    <!--<div class="layui-form-item">-->
        <!--<label class="layui-form-label">工单状态</label>-->
        <!--<div class="layui-input-inline">-->
            <!--<select name="state">-->
                <!--<option value="未分配">未分配</option>-->
                <!--<option value="处理中">处理中</option>-->
                <!--<option value="已处理">已处理</option>-->
            <!--</select>-->
        <!--</div>-->
    <!--</div>-->
    <div class="layui-form-item">
        <label class="layui-form-label">代码TAG</label>
        <div class="layui-input-inline" id="view">
        </div>
        <!--<div class="layui-input-inline">-->
            <!--<select lay-verify="required" name="tagName">-->
                <!--<option value="">请选择</option>-->
                <!--<option value="20200117-160811">20200117-160811</option>-->
                <!--<option value="20200117-134633">20200117-134633</option>-->
                <!--<option value="20200117-131823">20200117-131823</option>-->
            <!--</select>-->
        <!--</div>-->
        <!--<div class="layui-input-inline">-->
            <!--<select name="tagName" id="tagName" lay-verify="required">-->
                <!--<option value=""></option>-->
                <!--{{# layui.each(d.roles,function(index,item){ }}-->
                <!--<option value="{{item.value}}">{{item.value}}</option>-->
                <!--{{# }); }}-->
            <!--</select>-->
        <!--</div>-->
    </div>
    <div class="layui-form-item layui-hide">
        <input type="button" lay-submit lay-filter="layuiadmin-app-form-submit" id="layuiadmin-app-form-submit" value="确认添加">
        <input type="button" lay-submit lay-filter="layuiadmin-app-form-edit" id="layuiadmin-app-form-edit" value="确认编辑">
    </div>
</div>

<script id="tags-tpl" type="text/html">
    <select name="tagName" lay-verify="required" lay-filter="lf_role" >
        <option value="">请选择</option>
        {{# layui.each(d.tags,function(index,item){ }}
            <option value="{{item}}">{{item}}</option>
        {{# }); }}
    </select>
</script>

<script src="../../../layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '../../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form'], function(){
        var $ = layui.$,form = layui.form;
        var laytpl = layui.laytpl;
        function getQueryVariable(variable)
        {
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for (var i=0;i<vars.length;i++) {
                var pair = vars[i].split("=");
                if(pair[0] == variable){return pair[1];}
            }
            return(false);
        }
        var jobName = getQueryVariable("jobName");
        var jobType = getQueryVariable("jobType");
        $("#jobName").val(jobName);
        if(jobType === "beta"){
            $("#branchName").val("beta");
        }else {
            $("#branchName").val("master");
        }
        var staticData = {
            tags: []
        };
        //向服务端发送删除指令
        $.ajax({
            url: "/api/jenkins/gitTags?jobName="+jobName,
            type: 'get',
            data: {},
            success: function (data, textStatus) {
                if (textStatus == 'success') {
                    if (typeof(data.code) == "undefined") {
                        console.log("----code = undefined----"+data.code);
                        //登入成功的提示与跳转
                        layer.msg('登录状态已过期', {offset: '15px', icon: 2, time: 1000},
                            function () {
                                location.href = '/';
                            });
                    }
                    //console.log(data);
                    if(data.code == 0){
                        if(jobType === "beta"){
                            staticData.tags = new Array("beta");
                        }else {
                            staticData.tags = data.data;
                        }
                        console.log(jobType + "----staticData.tags----- "+JSON.stringify(staticData.tags));
                        //获取到数据之后再加载到选择框
                            laytpl($('#tags-tpl').html()).render(staticData, function(html){
                            var view = document.getElementById('view');
                            view.innerHTML = html;
                            form.render('select');
                        });
                    }
                }
            }
        });


        //自定义验证规则
        // form.verify({
        //     tagName: function(value){
        //         console.log("----value----- "+JSON.stringify(value));
        //         if(value.length < 1){
        //             return '代码TAG不能为空';
        //         }
        //     }
        // });
        //监听提交
        form.on('submit(layuiadmin-app-form-submit)', function(data){
            var field = data.field; //获取提交的字段
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            console.log(jobName + "----field----- "+JSON.stringify(field));

            //获取被选择的那个
            // data.field.RoleText=$("#tagName").find("option:selected").text();
            //console.log(data.field)
            var postData = {"jobName":jobName,
                "tagName":field.tagName,
                "publishType":"build",
                "number":0
            };
            console.log(postData)
            //提交 Ajax 成功后，关闭当前弹层并重载表格
            $.ajax({
                url: "/api/jenkins/buildJob",
                type: 'post',
                contentType:"application/json",
                dataType:'json',
                data: JSON.stringify(postData),
                success: function (data, textStatus) {
                    //console.log(data);
                    if (textStatus == 'success') {
                        if (typeof(data.code) == "undefined") {
                            console.log("----code = undefined----"+data.code);
                            //登入成功的提示与跳转
                            layer.msg('登录状态已过期', {offset: '15px', icon: 2, time: 1000},
                                function () {
                                    location.href = '/';
                                });
                        }
                        if(data.code != 0) {
                            alert(data.msg)
                            console.log(data.data);
                        }
                        layer.msg(data.msg);
                        parent.layui.table.reload('LAY-app-content-list'); //重载表格
                        parent.layer.close(index); //再执行关闭
                    }
                }
            })
        });
    })
</script>
</body>
</html>